using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using TraiderInformationUI.Infrastructure;
using TraiderInformationUI.Interfaces.Services;
using TraiderInformationUI.Models.Models;

namespace TraiderInformationUI.Modules.News.Article
{
    [Export(typeof(ArticleViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ArticleViewModel : NotificationObject
    {
        private string _companySymbol;
        private IList<NewsArticle> _articles;
        private NewsArticle _selectedArticle;
        private readonly INewsFeedService _newsFeedService;
        private readonly IRegionManager _regionManager;
        private readonly ICommand _showArticleListCommand;
        private readonly ICommand _showNewsReaderViewCommand;

        [ImportingConstructor]
        public ArticleViewModel(INewsFeedService newsFeedService, IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            if (newsFeedService == null)
                throw new ArgumentNullException("newsFeedService");

            if (regionManager == null)
                throw new ArgumentNullException("regionManager");

            if (eventAggregator == null)
                throw new ArgumentNullException("eventAggregator");

            _newsFeedService = newsFeedService;
            _regionManager = regionManager;

            _showArticleListCommand = new DelegateCommand(ShowArticleList);
            _showNewsReaderViewCommand = new DelegateCommand(ShowNewsReaderView);

            eventAggregator.GetEvent<TickerSymbolSelectedEvent>().Subscribe(OnTickerSymbolSelected, ThreadOption.UIThread);
        }

        public string CompanySymbol
        {
            get
            {
                return _companySymbol;
            }
            set
            {
                if (_companySymbol != value)
                {
                    _companySymbol = value;
                    RaisePropertyChanged(() => CompanySymbol);
                    OnCompanySymbolChanged();
                }
            }
        }

        public NewsArticle SelectedArticle
        {
            get { return _selectedArticle; }
            set
            {
                if (_selectedArticle != value)
                {
                    _selectedArticle = value;
                    RaisePropertyChanged(() => SelectedArticle);
                }
            }
        }

        public IList<NewsArticle> Articles
        {
            get { return _articles; }
            private set
            {
                if (_articles != value)
                {
                    _articles = value;
                    RaisePropertyChanged(() => Articles);
                }
            }
        }

        public ICommand ShowNewsReaderCommand { get { return _showNewsReaderViewCommand; } }

        public ICommand ShowArticleListCommand { get { return _showArticleListCommand; } }


        private void OnTickerSymbolSelected(string companySymbol)
        {
            CompanySymbol = companySymbol;
        }

        private void OnCompanySymbolChanged()
        {
            Articles = _newsFeedService.GetNews(_companySymbol);
        }

        private void ShowArticleList()
        {
            SelectedArticle = null;
        }

        private void ShowNewsReaderView()
        {
            _regionManager.RequestNavigate(RegionNames.SecondaryRegion, new Uri("/NewsReaderView", UriKind.Relative));
        }
    }
}
